
<template>
	<view>
	    <block v-if="dataBuffer">
			<view class="item"v-for="(res, index) in siteList" :key="res.id" >
				<u-icon name="close-circle-fill" :size="40" color="#ff5500" :index="res.id"  @click="del(index,res.id)"></u-icon>
				<view class="top" @click="chooseadd(res)" >
					<view class="name">{{ res.username }}</view>
					<view class="phone">{{ res.userphone }}</view>
					<view class="tag">
						<text class="red" v-if="res.status == 1">默认</text>
					</view>
				</view>
				<view class="bottom" >
					{{res.location+res.useraddress}}
					<u-icon name="edit-pen" :size="40" color="#999999" :index="res.id"  @click="editAddSite"></u-icon>
				</view>
			
			</view>
		</block>
		
		
		<view v-else>
			暂无收货地址信息!
		</view>
		<view class="addSite" @tap="toAddSite">
			<view class="add">
				<u-icon name="plus" color="#ffffff" class="icon" :size="30"></u-icon>新建收货地址
			</view>
		</view>
	</view>
</template>

<script>

export default {
	data() {
		return {
			siteList: [],//初始化地址数据
			dataBuffer:true
		};
	},
	onShow() {
		
	this.addresslist()
	},
	methods: {
		//选择地址
		 chooseadd(e){
			 uni.navigateTo({
			 	url:"/pages/order/order?address="+JSON.stringify(e)
			 })
		 },
		// 删除
		async del(index,id){
			let [,{confirm}]=await uni.showModal({
				title:"确定要删除吗？"
			})
			if(!confirm){
				retutn
			}
			let {token} = uni.getStorageSync("userinfo")
			let [err,res ] = await uni.request({
				url:this.$baseUrl+"/api/addressremove",
				header:{
					authorization:token
				},
				data:{
					id
				},
					method:"POST"
			})
			if(err){
					 uni.showToast({
						title:"出错了",
						icon:'error'
					 })
					 return
			}
			if(res.data.code==403){
					 uni.showToast({
						title:"登录过期",
						icon:'error'
				 })
			 setTimeout(()=>{
						 uni.navigateTo({
						url:"/pages/login/login"
				 })
			 },1000)
				 return
			}
			if(res.data.code!=200){
				 uni.showToast({
								title:"出错了",
								icon:'error'
				 })
				 return
			}
					
			this.siteList.splice(index,1)
			
			uni.showToast({
				title:"删除成功"
			})
			if(this.siteList.length==0){
			 this.dataBuffer=false
			}
			
		
		},
		
		
		// http://localhost:3000/api/addresslist
		async addresslist(){
			let {uid,token}=uni.getStorageSync("userinfo")
			let [err,res]=await uni.request({
				url:this.$baseUrl+"/api/addresslist",
				header:{
					 authorization:token
				},
				data:{
					uid
				}
			})
			console.log(res);
			if(err){
				 uni.showToast({
						title:"出错了",
						icon:"error"
					 })
			 return
			}
			if(res.data.code==403){
					uni.showToast({
					title:"登录过期",
					icon:"error"
				 })
			 setTimeout(()=>{
				 uni.navigateTo({
						 url:"/pages/login/login"
					  })
								 						 
				 },1000)
								 
				 return
								 
			}
			if(res.data.code !=200){
				 uni.showToast({
						title:"出错了",
						icon:"error"
					 })
								 
				 return
			}
			if(!res.data.list){
				this.dataBuffer=false
				return
			}
			this.dataBuffer=true
			
			this.siteList=res.data.list
			
		},
		
		// 跳转到修改 新增和修改是同一个页面
		//  参数:通过props传递的index值
		editAddSite(id){
			uni.navigateTo({
				url:'./addSite?id='+id
			})
		},
		// 跳转到新增地址页面
		toAddSite(){
			uni.navigateTo({
				url:'./addSite'
			})
		}
	}
};
</script>

<style lang="scss" scoped>
	// 引入样式
	@import  '@/static/css/address.scss';
</style>
